home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10541 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  104 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie question re: struct pointers & member access
  5. Date: Fri, 08 Mar 1996 16:07:30 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4hplup$f3@news.halcyon.com>
  8. References: <4hn0v2$7q5@aahz.magic.mb.ca>
  9. NNTP-Posting-Host: blv-pm3-ip14.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. jranta@astral.magic.ca (jranta) wrote:
  13.  
  14. >Hi!
  15. >I am attempting to write a simple program to prepare myself for a C++ course
  16. >at school.  The program is to request from the user how many cars to catalog
  17. >and to then record this many cars.  I'm using the "new" operator to allocate
  18. >the proper number of structures; there doesn't seem to be a problem with input
  19. >but when I attempt to ouput using the pointer, all I get is NULL for each
  20. >record.
  21.  
  22. >Here is the code so far:
  23.  
  24. >#include<iostream.h>
  25. >#include<conio.h>
  26.  
  27. >const int ArSize = 30;
  28. >void enter(int t);
  29.  
  30. >struct car
  31. >    {
  32. >     char make[ArSize];
  33. >     int year;
  34. >    };
  35. >int main(void)
  36. >{
  37. > int total;
  38. > clrscr();
  39. > cout<<"How many cars do you wish to catalog?";
  40. > cin>>total;
  41. > enter(total);
  42.  
  43. >return 0;
  44. >}
  45. >void enter(int t)
  46. >{
  47. > car *ptr = new car[t];
  48. > for(int a=0;a<t;a++,ptr++)
  49. >    {
  50. >        cin.get(); //to clear input queue(alt. between strings & numbers).
  51. >        cout<<"Car #"<<a+1;
  52. >    cout<<"\nPlease enter make: ";     
  53. >    cin.getline(ptr->make,ArSize);
  54. >    cout<<"\nEnter year: ";
  55. >    cin>>ptr->year;
  56. >    } 
  57. >return;
  58. >}
  59.  
  60. >I attempted to use another "for" loop to display output, but I think that I'm
  61. >not setting the pointer properly at the first record.  This is where my 
  62. >confusion lies: how do I set the pointer at the beginning and then increment
  63. >it?  My output was all NULL, as mentioned above, so I guess that I was 
  64. >displaying from the last pointer position and on, resulting in garbage.
  65.  
  66. >Any help/hints would be appreciated (I know this concept is *very* important
  67. >for OOP, which is what my course is.).
  68.  
  69. >If preferred and/or convenient, e-mail me at:
  70.  
  71. >jranta@astral.magic.ca
  72.  
  73. >Thanks a lot in advance,
  74. >Jouni
  75.  
  76. The enter() function doesn't seem to be doing any output at all, nor
  77. returning the allocated pointer of cars, but I suspect the problem is
  78. you were trying to re-use ptr?
  79.  
  80. Perhaps you need a second car *, one to march through inputting, the
  81. other for outputting.  
  82.  
  83. car  *  pCars,
  84.     pACar;
  85.  
  86. pACar = pCars = new car[ N ];
  87. if( NULL == pCars )
  88.     // oopse, out of memory
  89. for( a=0; a<N; a++, pACar++ )
  90. {
  91.     // input into pACar
  92. }
  93.  
  94. pACar = pCars;   // rest to beginning
  95. for( a=0; a<N; a++, pACar++)
  96. {
  97.     // output...
  98. }
  99.  
  100. Does this help?
  101.  
  102.                 --Norm 
  103.  
  104.